package com.tsingtree.modular.service.impl;

import com.tsingtree.modular.entity.NanImMsgMaster;
import com.tsingtree.modular.entity.NanSellerTag;
import com.tsingtree.modular.entity.NanUserDevice;
import com.tsingtree.modular.mapper.NanImMsgMasterMapper;
import com.tsingtree.modular.model.params.NanImMsgMasterParam;
import com.tsingtree.modular.model.result.NanImMsgMasterResult;
import  com.tsingtree.modular.service.NanImMsgMasterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsingtree.modular.service.NanSellerTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * <p>
 * 私信 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2025-05-26
 */
@Service
public class NanImMsgMasterServiceImpl extends ServiceImpl<NanImMsgMasterMapper, NanImMsgMaster> implements NanImMsgMasterService {

    @Autowired
    private NanSellerTagService nanSellerTagService;

    @Override
    public void add(NanImMsgMasterParam param){
        NanImMsgMaster entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanImMsgMasterParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanImMsgMasterParam param){
        NanImMsgMaster oldEntity = getOldEntity(param);
        NanImMsgMaster newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanImMsgMasterResult findBySpec(NanImMsgMasterParam param){
        return null;
    }

    @Override
    public List<NanImMsgMasterResult> findListBySpec(NanImMsgMasterParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanImMsgMasterParam param){
        Page pageContext = getPageContext();
        IPage<NanImMsgMasterResult> page = this.baseMapper.customPageList(pageContext, param);
        Map<Integer, String> tagMap = nanSellerTagService.list().stream().collect(Collectors.toMap(NanSellerTag::getId, NanSellerTag::getName));
        page.getRecords().forEach((obj) -> {
            String tag = obj.getTag();
            if(StringUtils.hasText(tag)){
                String[] tags = tag.split(",");
                StringBuilder names = new StringBuilder();
                for(String tagStr : tags){
                    int key = Integer.parseInt(tagStr.substring(1, tagStr.length() - 1));
                    String name = tagMap.get(key);
                    names.append(" | ").append(name);
                }
                obj.setTagStr(names.substring(3));
            }
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NanImMsgMasterParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanImMsgMaster getOldEntity(NanImMsgMasterParam param) {
        return this.getById(getKey(param));
    }

    private NanImMsgMaster getEntity(NanImMsgMasterParam param) {
        NanImMsgMaster entity = new NanImMsgMaster();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
